<?php
class Gmapsprofile_FriendsController extends Core_Controller_Action_Standard
{
  protected $data_json;
  protected $options;
  
  protected function _executeSearch()
  {
	if (!empty($this->options)) {
	  $user = Engine_Api::_()->getItem('user', (int) $this->options);
	}
	else 
      $user = Engine_Api::_()->user()->getViewer();
	$select = $user->membership()->getMembersOfSelect();
	$friends = $paginator = Zend_Paginator::factory($select);
	$paginator->setItemCountPerPage(10000000);
	$ids = array();
    foreach( $friends as $friend ) {
      $ids[] = $friend->resource_id;
    }
    
    $users = array();
    
    if (count($ids)) {
	  $table = Engine_Api::_()->getDbtable('markers', 'gmapsprofile');
	  $select = $table->select();
	  $select->where('user_id IN(?)', (array) $ids);	
	  $users = $table->fetchAll($select);
    }  

    $data = $this->geocodingData($users);
    $this->data_json = $data;

    return true;
  }
 
  public function jsondataAction()
  {
   $this->options = Zend_Json_Decoder::decode($_POST['subject']);
   if( !$this->_executeSearch() ) {
     throw new Exception('error');
   }
   print_r($this->data_json);
   exit;
  }
  
  public function contentAction()
  {
	$data = '';
	$point = ', ';
	$page = isset($_GET['page']) ? (int) $_GET['page'] : 1;
	$subject = isset($_GET['subject']) ? (int) $_GET['subject'] : 0;
	
	$address = Engine_Api::_()->gmapsprofile()->addressUser((int) $_GET['id']);
	$same_address = Engine_Api::_()->gmapsprofile()->sameAddress($address['lat'], $address['lng'], $subject); 
	$pager = '';
	$count_same = count($same_address);

	if ($count_same > 1) {  
	  $prev = ($page != 1) ? "<a href='javascript:void(0);' onclick='loadInfo(".$same_address[$page-2]->user_id.", ".($page-1).")'>&laquo; ".Zend_Registry::get('Zend_Translate')->_('previous')."</a>&nbsp;" : "";
	  $next = ($page == $count_same) ? "" : "&nbsp;<a href='javascript:void(0);' onclick='loadInfo(".$same_address[$page]->user_id.", ".($page+1).", ".$subject.")'>".Zend_Registry::get('Zend_Translate')->_('next')." &raquo;</a>";
	  $pager = "<table border=0 cellpadding=0 width=100%><tr><td valign=top collspan=2 style='text-align:center' id=pager>".$prev.$page." of ".$count_same.$next."</td></tr></table>";
	  $user = Engine_Api::_()->getItem('user', $same_address[$page-1]->user_id);
	}
	else {
	  $user = Engine_Api::_()->getItem('user', (int) $_GET['id']);
	}  
	$params_member = $this->view->gmapsprofileFields($user);
	
	$point = ', ';
	if (empty($params_member['gender']) || empty($params_member['birthdate'])) $point="";	
	  $gender_bd = $this->view->translate(strtolower($params_member['gender'])).$point.$params_member['birthdate'];	
	  
	if( $this->view->gmapsprofileOnline($user) ) {
	  $online = "<div class=online_user_small>".$this->view->translate('Online')."</div>";
	}  
	else {
	  $online = "<div class=offline_user_small>".$this->view->translate('Offline')."</div>";   
	}  
    		  
	$compose = '';
	$friend = '';
	$block = '';
	$report = '';
	if( $this->view->viewer()->getIdentity() )
      $compose = $this->view->gmapsprofileCompose($user);
    if( $this->view->viewer()->getIdentity() )
      $friend = $this->view->gmapsprofileFriendship($user);
    if( $this->view->viewer()->getIdentity() )
	  $block = $this->view->gmapsprofileBlock($user);
 	if( $this->view->viewer()->getIdentity() )
      $report = $this->view->gmapsprofileReport($user);	  

	$data = '<div id=gmap_loading></div><div id=content_info><table width=250px border=0 cellpadding=3 id=info><tr><td width=50px><div class=icon_small_user>'.$this->view->htmlLink($user->getHref(), $this->view->itemPhoto($user, "thumb.icon")).$online.'</div></td><td valign=top style=padding-left:5px;><span class=displayname_user>'.$this->view->htmlLink($user->getHref(), $user->getTitle()).'</span><br /><span class=gender_user>'.$gender_bd.'</span><br /><span class=location_user>'.$params_member["territories"].'</span><br /><div class=link_members_gmap>'.$compose.$friend.$block.$report.'</div></td></tr></table><br />' . $pager. '</div>';
    
    print_r($data);
    exit;
  }  
  
  public function geocodingData($users)
  { 
	$count_data = 0;
	$prefix = '';
	$data = '';
	
    foreach( $users as $user ) {
		$count_data++;
	  
		$data .= $prefix . "{'user_id':".$user->user_id.",'longitude':".$user->lng.",'latitude':".$user->lat."}";
		$prefix = ',';
	} 
	return '{\'count\':'.$count_data.',\'members\':['.$data.']}';  
  }	  
}